package com.spring.web.adminmanage.repository;

import com.spring.web.adminmanage.entities.User;
import com.spring.web.adminmanage.myMapper.MyMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户dao层
 * @author xiankejin
 * @date 2017-8-30
 */
public interface WebUserDao extends MyMapper<User> {

	/**
	 * 查询user列表数据
	 * @param user
	 * @return
	 */
	List<User> queryUserResult(User user);

	/**
	 * 删除user对象
	 * @param user
	 */
	@Select("update api_user set del_flag='1' where id = #{id}")
	void delUser(User user);

	/**
	 * 通过id获取用户
	 * @param user
	 * @return
	 */
	@Select("select t.*,t.sys_pwd as sysPwd, t.user_key as userKey ,t.person_phone as phone,t.person_name as nickname, t.password as userpassword, a.id as 'role.id', a.rolename as 'role.rolename' from api_user t left join role a on t.role = a.id where t.id=#{id}")
	User getUserById(User user);

	/**
	 * 通过id更新用户
	 * @param user
	 */
	@Select("update api_user set username=#{username}, password=#{userpassword}, state=#{state}, person_name=#{nickname}, person_phone=#{phone}, user_key=#{userKey}, salt=#{salt},"
			+ "role = #{role.id}, remark=#{remark}, sys_pwd=#{sysPwd} where id= #{id}")
	void updateUser(User user);

	/**
	 * 新增后台用户
	 * @param user
	 */
	@Select("insert into api_user (username, password, state, person_name, person_phone,role, del_flag, user_key, salt, sys_pwd) values(#{username},#{userpassword}, 1, #{nickname}, #{phone}"
			+ ", #{role.id}, '0', #{userKey}, #{salt}, #{sysPwd})")
	void saveUser(User user);

	/**
	 * 根据用户名获取用户对象
	 * @param user
	 */
	@Select("select a.*,a.user_key as userKey, a.sys_pwd as sysPwd,a.password as userpassword, a.person_phone as phone,a.person_name as nickname, b.id as 'role.id', b.rolename as 'role.rolename',b.secrecy as 'role.secrecy' from api_user a left join role b on b.id = a.role where a.username = #{username}")
	User getUserByUsername(User user);

}